package com.andy.wan.android.mvp.base;

import com.andy.ui.widget.PageStateView;
import com.andy.wan.android.mvp.bean.BaseResponse;
import com.blankj.utilcode.util.ToastUtils;

import androidx.annotation.NonNull;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * @Author: luofan
 * @CreateDate: 2020/5/13 15:24
 * @Description:
 */
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {

    private boolean mShowErrorToast;

    public BaseObserver() {
        mShowErrorToast = true;
    }

    public BaseObserver(boolean showErrorToast) {
        mShowErrorToast = showErrorToast;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {

    }

    @Override
    public void onNext(@NonNull BaseResponse<T> response) {
        if (response.getErrorCode() == 0) {
            onGetData(response.getData());
        } else {
            ToastUtils.showLong(response.getErrorMsg());
        }
    }

    @Override
    public void onError(Throwable e) {
        ToastUtils.showLong(e.getMessage());
    }

    @Override
    public void onComplete() {

    }

    protected abstract void onGetData(T data);
}
